package club.ljwang.controller;

import club.ljwang.constant.MessageConstant;
import club.ljwang.entity.QueryPageBean;
import club.ljwang.entity.Result;
import club.ljwang.exception.RoleException;
import club.ljwang.pojo.Menu;
import club.ljwang.pojo.Permission;
import club.ljwang.pojo.Role;
import club.ljwang.pojo.RoleList;
import club.ljwang.service.RoleService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RequestMapping("role")
@RestController
public class RoleController {

    @Reference
    private RoleService roleService;

    @RequestMapping("findAllPermission")
    public Result findAllPermission(){
        List<Permission> permissions = roleService.findAllPermission();
        return new Result(true,"",permissions);
    }

    @RequestMapping("findAllMenu")
    public Result findAllMenu(){
        List<Menu> menuList = roleService.findAllMenu();
        return new Result(true,"",menuList);
    }

    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        List<Role> roles =roleService.findPage(queryPageBean);
        return new Result(true,"",roles);
    }

    @RequestMapping("add")
    public Result add(@RequestBody Role role){
        try {
            roleService.add(role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增角色失败");
        }
        return new Result(true, "新增角色成功");
    }

    @RequestMapping("delete")
    public Result delete(Integer id){
        try {
            roleService.delete(id);
        } catch (RoleException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除角色失败");
        }
        return new Result(true,"删除角色成功");
    }

    /**
     * 编辑角色之数据回显
     * @return
     */
    @RequestMapping("findByIdForEdit")
    public Result findByIdForEdit(Integer id){
        RoleList roleList = roleService.findByIdForEdit(id);
        return new Result(true,"",roleList);
    }

    @RequestMapping("edit")
    public Result edit(@RequestBody Role role){
        roleService.edit(role);
        return new Result(true,"编辑角色权限成功");
    }
}
